import sys
import MySQL
import Events
import People
import datetime


class PeopleInSubGroup:
    def __init__(self, SQLConn, dBName = "PEOPLE_IN_SUBGROUP"):
        self.SQLConn = SQLConn
        self.dBName = dBName
        
    def AddPersonToSubGroup(self, Person_ID, EventID, SubGroupID, Proportion):
        self.SQLConn.SendCommand("""
         INSERT INTO %s (Eve_id, Peo_id, SubG_id, Prop, Accept)
         VALUES ('%d', '%d', '%d', '%f', 0)
        """ %(self.dBName, EventID, Person_ID, SubGroupID, Proportion))
    
    def UpdatePerson(self, PID, EID, SubG_id, Prop):
         self.SQLConn.SendCommand("""UPDATE %s
                                SET Prop = '%d',
                                WHERE SubG_id = '%d' AND EID = '%d' AND PID = '%d'
                                """ % (self.dBName, Prop, SubG_id, EID, PID))
    
    def RemovePersonFromSubGroup(self, EID, PID, SubGroupID):
        self.SQLConn.SendCommand("""    
         DELETE FROM %s where SubG_id=%d AND Eve_id=%d AND Peo_id=%d
         """ % (self.dBName, SubGroupID, EID,PID))
        
    def RemovePerson(self, PID):
        self.SQLConn.SendCommand("""    
         DELETE FROM %s where Peo_id=%d
         """ % (self.dBName, PID))
        
    def RemoveEvent(self, EID):
        self.SQLConn.SendCommand("""    
         DELETE FROM %s where Eve_id=%d
         """ % (self.dBName, EID))
    
    def RemovePersonFromEvent(self, EID, PID):
        self.SQLConn.SendCommand("""    
         DELETE FROM %s where Eve_id=%d AND Peo_id = %d
         """ % (self.dBName, EID, PID))
        
    def RemoveSubGroup(self, SubG_id):
        self.SQLConn.SendCommand("""    
         DELETE FROM %s where SubG_id=%d
         """ % (self.dBName, SubG_id))
        
    def GetAllPeopleInSubgroupOnEvent(self, SubG_id, EID):
        self.SQLConn.SendCommand("""SELECT Peo_id, Prop FROM %s WHERE SubG_id = %d AND Eve_id = %d"""%(self.dBName, SubG_id, EID))
        rows = self.SQLConn.FetchAll ()
        People = []
        for Person in rows:
            People.append([Person[0], Person[1]])
        return People
    
    def GetAcceptState(self, PID, EID):
        self.SQLConn.SendCommand("""SELECT SubG_id, Accept FROM %s WHERE Peo_id = %d AND Eve_id = %d"""%(self.dBName, PID, EID))
        rows = self.SQLConn.FetchAll ()
        res = {}
        for SG in rows:
            res[SG[0]] = SG[1]
        return res
    
    def SetAcceptState(self, PID, SGID, Accept):
        self.SQLConn.SendCommand("""UPDATE %s
                                SET Accept = '%d',
                                WHERE SubG_id = '%d' AND PID = '%d'
                                """ % (self.dBName, Accept, SGID, PID))
        
    
    
    def GetAllSubGroupsPersonInOnEvent(self, PID, EID):
        self.SQLConn.SendCommand("""SELECT SubG_id, Prop FROM %s WHERE Peo_id = %d AND Eve_id = %d"""%(self.dBName, PID, EID))
        rows = self.SQLConn.FetchAll ()
        Expenses = []
        for Exp in rows:
            Expenses.append([Exp[0], Exp[1]])
        return Expenses
        
    
    #def GetAllPersonOn(self, PID):
    #    self.SQLConn.SendCommand("""SELECT Eve_id, DatesOnEvent FROM PEOPLE_EVENTS WHERE Peo_id = %d"""%PID)
    #    rows = self.SQLConn.FetchAll ()
    #    Events = []
    #    for Event in rows:
    #        Events.append([Event[0], GetDateArrayFromString(Event[1])])
    #    return Events
    #
    #def GetDatesPersonOnEvent(self, EID, PID):
    #    self.SQLConn.SendCommand("""SELECT DatesOnEvent FROM PEOPLE_EVENTS WHERE Peo_id = %d AND Eve_id = %d"""%(PID,EID))
    #    rows = self.SQLConn.FetchAll ()
    #    print rows
    #    return GetDateArrayFromString(rows[0][0])
    #    
        
        
    #
    #def UpdateEventPeople(self, EID, PID, Dates):
    #   self.SQLConn.SendCommand("""UPDATE PEOPLE_EVENTS
    #                            SET DatesOnEvent = '%s'
    #                            WHERE Peo_id = %d
    #                            AND Eve_id = %d
    #                            """ % (FormatDateArrayToString(Dates), PID, EID))
    #   
    #def GetPersonsManDays(self, EID = -1, PID = -1, Dates = ''):
    #    if Dates == '':
    #        Dates= self.GetDatesPersonOnEvent(EID, PID)
    #    
    #    ManDays = 0
    #    for Date in Dates:
    #        ManDays = ManDays + (1.0 * (Date[1]/100.0))
    #    
    #    return ManDays
    #
    #def GetTotalEventManDays(self, EID):
    #    PeopleOnEvent= self.GetAllPeopleOnEvent(EID)
    #    ManDays = 0
    #    for PID, Dates in PeopleOnEvent:
    #        for Date in Dates:
    #            ManDays = ManDays + (1.0 * (Date[1]/100.0))
    #    
    #    return ManDays
        
       
    
if __name__ == '__main__':
    SQL = MySQL.Connection()
    
        
    
    SQL.SendCommand ("DROP TABLE IF EXISTS PEOPLE_IN_SUBGROUP")
    SQL.SendCommand("""CREATE TABLE PEOPLE_IN_SUBGROUP(
                    Eve_id int,
                    SubG_id int,
                    Peo_id int,
                    Prop float,
                    Accept int)
                    """)



    #E = Events(SQL)
    #exit = False
    #while not exit:
    #    Input = raw_input("Now What? : ")
    #    if Input == 'exit':
    #        exit = True
    #    if Input == 'new':
    #        Event_Name = raw_input("Event Name : ")
    #        Event_Dates = raw_input("Event Dates : ")
    #        People = raw_input("People : ")
    #        E.AddEvent(Event_Name, Event_Dates, People)
    #        
    #    if Input == 'del':
    #        EID = int(raw_input("Number : "))
    #        E.RemoveEvent(EID)
    #        
    #    if Input == 'print':
    #        Ev =  E.GetAllEvents()
    #        for w in Ev:
    #            print w.EID, w.EventName, w.DateStr
    #            
    #    if Input == 'Update':
    #        ID = int(raw_input("EID : "))
    #        name = raw_input("name : ")
    #        E.UpdateEvent(ID, name)
    #        
    #print "Finished"
    #  
    #SQL.close()  
    #
    
    
  
    
        